package kpi.java_training.testing.questions;

import kpi.java_training.testing.education.TypeScore;
import kpi.java_training.testing.services.StringServices;
import java.util.List;

/**
 * class implements questions where answer is integer
 */
public class IntQuestion extends Question {
    protected final int NOT_ANSWERED = -999999999;
    protected List<String> answerVariants;
    protected List<Integer> correctAnswers;
    private int answer;

    public IntQuestion(String questionMessage, List<String> answerVariants, List<Integer> correctAnswers, int level) {
        super(questionMessage);
        this.correctAnswers = correctAnswers;
        this.answerVariants = answerVariants;

        this.level = level;
        answer = NOT_ANSWERED;
    }

    @Override
    protected String getQuestionHelp() {
        return "Type number as the answer.";
    }

    @Override
    public String toString() {
        String str = getQuestionHelp();

        str = StringServices.makeString(str, getQuestionMessage());
        for (int i = 0; i < answerVariants.size(); i++) {
            str = StringServices.makeString(str, (String.format("%d. %s", i + 1, answerVariants.get(i))));
        }
        return str;
    }

    @Override
    public void setAnswer(String answer) {
        setOriginalAnswer(answer);
        try {
            this.answer = Integer.parseInt(answer);

        } catch (Exception e) {
            this.answer = NOT_ANSWERED;
        }

    }

    @Override
    public boolean isCorrect() {
        for (int correct : correctAnswers) {
            if (correct == answer) {
                return true;
            }
        }
        return false;
    }


    @Override
    public void clearAnswer() {
        answer = NOT_ANSWERED;
    }
    @Override
    protected int getQuestionType() {
        return Question.INTEGER;
    }

    @Override
    public String getQuestionSaveData() {
        String str = super.getQuestionSaveData();
        str = StringServices.makeString(str, StringServices.getSaveDataFromStrings(answerVariants));
        str = StringServices.makeString(str, StringServices.getSaveDataFromIntegers(correctAnswers));
        return str;
    }
}
